{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 二、基本数据类型\n",
    "# 1、数字类型组成"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.1 整数——不同进制的转换\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0b10000 0o20 0x10\n"
     ]
    }
   ],
   "source": [
    "16==0b10000==0o20==0x10\n",
    "#十进制与其他进制的转换\n",
    "a = bin(16)\n",
    "b = oct(16)\n",
    "c = hex(16)\n",
    "print(a,b,c)\n",
    "#注：上述转换完的结果都是字符串\n",
    "#其他进制转10进制\n",
    "d = int (a,2)\n",
    "e = int (b,8)\n",
    "f = int (c,16)\n",
    "print(d,e,f)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T11:50:22.979118500Z",
     "start_time": "2024-11-16T11:50:22.932731100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.2 浮点数——不确定性(精度问题)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "False"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(0.1+0.2)==0.3\n",
    "#四舍五入获得精确解\n",
    "b = round(0.1+0.2,2)#保留2位小数\n",
    "print(b)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T11:53:22.203983700Z",
     "start_time": "2024-11-16T11:53:22.134572100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 1.3 复数——a+bi\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "(3+1j)"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3 + 4j\n",
    "#注：虚部为1时，需要显式写出\n",
    "3 + 1j"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T12:09:11.279008Z",
     "start_time": "2024-11-16T12:09:11.211007800Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 2.基本运算\n",
    "## 2.1 基本运算符"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#乘方\n",
    "a = 2**3\n",
    "print(a)\n",
    "#整数商:x/y 向下取整\n",
    "b = 10//3\n",
    "print(b)\n",
    "#余数，模\n",
    "a = 10%3\n",
    "print(a)\n",
    "#乘方\n",
    "a = 2**3\n",
    "print(a)\n",
    "#幂"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.2数字运算操作函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "#绝对值\n",
    "abs(-1)\n",
    "# 幂次方\n",
    "pow(2,3)\n",
    "pow(2,3,4)#2^3%4\n",
    "#四舍五入，round\n",
    "round(3.1415926,2)\n",
    "\n",
    "# divmod 整数商和余数\n",
    "c = divmod(10,3)#10//3,10%3\n",
    "print(c)\n",
    "\n",
    "#序列最大最小值\n",
    "d = [1,2,3,4,5]\n",
    "print(\"max:\",max(d))\n",
    "print(\"min:\",min(d))\n",
    "\n",
    "#求和,参数必须是元组或列表\n",
    "sum(d)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.3转义字符\n",
    "在双引号前加反斜杠，代表把“输出"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\"python\"\n"
     ]
    }
   ],
   "source": [
    "print(\"\\\"python\\\"\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T12:21:57.329931400Z",
     "start_time": "2024-11-16T12:21:57.276550100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3.字符串\n",
    "## 3.1 字符串的性质\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p y t\n",
      "n o h\n",
      "pytho\n",
      "pytho\n",
      "python\n"
     ]
    }
   ],
   "source": [
    "#正向索引\n",
    "a = \"python\"\n",
    "print(a[0],a[1],a[2])\n",
    "#反向索引——从-1开始递减，最后一个为-1\n",
    "print(a[-1],a[-2],a[-3])\n",
    "#字符串切片 变量名[起始下标:结束下标:切片间隔]含前不含后\n",
    "print(a[0:5:1])\n",
    "\n",
    "#起始位置从0开始可以省略\n",
    "print(a[:5])\n",
    "\n",
    "#起始位置结束位置都省略\n",
    "print(a[:])\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T13:42:27.604927800Z",
     "start_time": "2024-11-16T13:42:27.547353900Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.2 反向切片"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "nohtyp\n",
      "nohtyp\n",
      "nohtyp\n"
     ]
    }
   ],
   "source": [
    "s = \"python\"\n",
    "print(s[-1:-7:-1])\n",
    "#省略起始位置，默认从-1开始\n",
    "print(s[:-7:-1])\n",
    "#省略起始位置和结束位置，默认从最后一个切片到第一个\n",
    "print(s[::-1])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T13:46:38.514518Z",
     "start_time": "2024-11-16T13:46:38.476070300Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.3 字符串的拼接"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "pythonworld\n",
      "python world\n",
      "pythonpythonpython\n",
      "True\n",
      "p\n",
      "y\n",
      "t\n",
      "h\n",
      "o\n",
      "n\n",
      ",\n",
      "w\n",
      "o\n",
      "r\n",
      "l\n",
      "d\n",
      ",\n",
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n"
     ]
    }
   ],
   "source": [
    "a = \"python\"\n",
    "b = \"world\"\n",
    "print(a + b)\n",
    "print(a + \" \" + b)\n",
    "\n",
    "#字符串复制\n",
    "print(a*3)\n",
    "\n",
    "#成员运算\n",
    "a = \"python,world,hello\"\n",
    "print(\"hello\" in a)\n",
    "\n",
    "#遍历字符串\n",
    "for i in a:\n",
    "    print(i)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T13:51:08.529779500Z",
     "start_time": "2024-11-16T13:51:08.508783700Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.4字符串处理函数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "97\n",
      "20013\n",
      "22269\n",
      "a\n",
      "中\n",
      "中录\n"
     ]
    }
   ],
   "source": [
    "# 字符串长度\n",
    "print(len(a))\n",
    "\n",
    "# 字符编码\n",
    "#将字符转换为unicode编码\n",
    "print(ord(\"a\"))\n",
    "print(ord(\"中\"))\n",
    "print(ord(\"国\"))\n",
    "\n",
    "#将unicode编码转换为字符\n",
    "print(chr(97))\n",
    "print(chr(20013))\n",
    "print(chr(20013)+chr(24405))\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-16T13:54:52.164036500Z",
     "start_time": "2024-11-16T13:54:52.098114100Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.5字符串的处理方法\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['python', 'world', 'hello']\n",
      "python,world,hello\n",
      "python,world,hello\n",
      "worldhello\n",
      "worldhello     \n",
      "   worldhello\n",
      ",count: 2\n"
     ]
    }
   ],
   "source": [
    "#字符串的分割--字符串.split()\n",
    "a = \"python,world,hello\"\n",
    "a_list = a.split(\",\")#参数是用什么来分割字符串\n",
    "print(a_list)\n",
    "print(a)\n",
    "\n",
    "\n",
    "#字符串的连接--字符串.join()\n",
    "a_list = [\"python\",\"world\",\"hello\"]\n",
    "a = \",\".join(a_list)#join中前面的符号是聚合后用什么连接\n",
    "print(a)\n",
    "\n",
    "#删除两端指定定字符\n",
    "a = \"   worldhello     \"\n",
    "print(a.strip(\" \"))#参数是指定的要删除的字符\n",
    "print(a.lstrip(\" \"))\n",
    "print(a.rstrip(\" \"))\n",
    "\n",
    "#字符串统计\n",
    "a = \"python,world,hello\"\n",
    "print(\",count:\",a.count(\",\"))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-18T13:52:07.720865700Z",
     "start_time": "2024-11-18T13:52:07.677702200Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 四、类型判别及转换\n",
    "## 4.1类型判别\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# type\n",
    "a = 10\n",
    "print(type(a))\n",
    "# isinstance,承认继承关系，看p是不是Person的实例\n",
    "class Person:\n",
    "    pass\n",
    "p = Person()\n",
    "print(isinstance(p,Person))\n",
    "\n",
    "#判断字符串是不是只由字母组成\n",
    "a = \"python\"\n",
    "print(a.isalpha())\n",
    "#判断字符串是不是只由数字组成\n",
    "print(a.isdigit())\n",
    "#判断字符串是不是只由字母和数字组成\n",
    "print(a.isalnum())\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.2类型转换"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "my age is +str(age)\n",
      "10\n",
      "20.1\n",
      "10.0\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "#数字转字符串\n",
    "age=20\n",
    "print(\"my age is +str(age)\")\n",
    "\n",
    "#字符串转数字\n",
    "a = \"10\"\n",
    "b=\"20.1\"\n",
    "print(int(a))#只能整数\n",
    "print(float(b))\n",
    "print(float(a))\n",
    "print(eval(a))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-18T14:07:12.340955Z",
     "start_time": "2024-11-18T14:07:12.320349Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
